package com.example.zhengchaojun.arcgis.basicesTest;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.mapping.view.SketchCreationMode;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.mapping.view.SketchGeometryChangedEvent;
import com.esri.arcgisruntime.mapping.view.SketchGeometryChangedListener;
import com.esri.arcgisruntime.mapping.view.SketchStyle;
import com.example.zhengchaojun.arcgis.R;
import com.example.zhengchaojun.arcgis.map.BaseArcgisMapActivity;

/**
 * 类描述：
 * 作者：郑朝军 on 2018/5/10
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2018/5/10
 * 修改备注：
 */
public class MearueActivity extends BaseArcgisMapActivity
{
    private TextView m_textView;
    private Handler mHandler;

    private SketchEditor mainSketchEditor;
    private SketchStyle mainSketchStyle;

    public int getLayoutID()
    {
        return R.layout.activity_meaure;
    }

    public void initView()
    {
        super.initView();
        m_textView = findViewById(R.id.tv_text);

        mHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                super.handleMessage(msg);
                m_textView.setText(msg.obj.toString());
            }
        };
    }

    public void initData()
    {
        mainSketchEditor = new SketchEditor();
        mainSketchStyle = new SketchStyle();
        mainSketchEditor.setSketchStyle(mainSketchStyle);
        mainSketchEditor.addGeometryChangedListener(new SketchGeometryChangedListener()
        {
            @Override
            public void geometryChanged(SketchGeometryChangedEvent sketchGeometryChangedEvent)
            {
                Geometry geometry = sketchGeometryChangedEvent.getGeometry();
                if (geometry == null) return;
                GeometryType type = geometry.getGeometryType();
                Message message = Message.obtain(mHandler);//利用Handle操作UI线程
                switch (type)
                {
                    case POINT:
                        Point point = (Point) GeometryEngine.project(geometry, SpatialReference.create(4326));
                        String point_str = "经度：" + point.getX() + "\n纬度：" + point.getY();
                        message.obj = point_str;
                        mHandler.sendMessage(message);
                        break;
                    case POLYLINE:
                        double length = GeometryEngine.length((Polyline) geometry);
                        String length_str = "长度：" + getLengthString(length);
                        message.obj = length_str;
                        mHandler.sendMessage(message);
                        break;
                    case POLYGON:
                        double area = GeometryEngine.area((Polygon) geometry);
                        String area_str = "面积：" + getAreaString(area);
                        message.obj = area_str;
                        mHandler.sendMessage(message);
                        break;
                }
            }
        });

        m_mapView.setSketchEditor(mainSketchEditor);

    }

    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.line:
                mainSketchEditor.clearGeometry();
                mainSketchEditor.start(SketchCreationMode.POLYLINE);
                break;
            case R.id.point:
                mainSketchEditor.clearGeometry();
                mainSketchEditor.start(SketchCreationMode.POINT);
                break;
            case R.id.btn_polygn:
                mainSketchEditor.clearGeometry();
                mainSketchEditor.start(SketchCreationMode.POLYGON);
                break;
            case R.id.clear:
                mainSketchEditor.clearGeometry();
                break;
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        m_mapView.setSketchEditor(null);
        mainSketchEditor.stop();
    }

    /**
     * 获取长度信息
     *
     * @param dValue
     * @return
     */
    private String getLengthString(double dValue)
    {
        long length = Math.abs(Math.round(dValue));
        String sLength = "";
        if (length > 1000)
        {
            double dArea = length / 1000.0;
            sLength = Double.toString(dArea) + "千米";//平方公里
        }
        else
        {
            sLength = Double.toString(length) + "米";//平方米
        }
        return sLength;
    }

    /**
     * 获取面积信息
     *
     * @param dValue
     * @return
     */
    private String getAreaString(double dValue)
    {
        long area = Math.abs(Math.round(dValue));
        String sArea = "";
        // 顺时针绘制多边形，面积为正，逆时针绘制，则面积为负
        if (area >= 1000000)
        {
            double dArea = area / 1000000.0;
            sArea = Double.toString(dArea) + "平方千米";//平方公里
        }
        else
            sArea = Double.toString(area) + "平方米";//平方米
        return sArea;
    }
}
